<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>出入库信息管理</title>
<meta name="decorator" content="default"/>
<meta http-equiv="Expires" CONTENT="0">
<meta http-equiv="Cache-Control" CONTENT="no-cache">
<meta http-equiv="Pragma" CONTENT="no-cache">
<link href="${ctxStatic}/bootstrap-editable/css/bootstrap-editable.css" rel="stylesheet" />
<link href="${ctxStatic}/bootstrap-table/bootstrap-table.min.css" rel="stylesheet" />
<script src="${ctxStatic}/bootstrap-editable/js/bootstrap-editable.js"></script>
<script src="${ctxStatic}/bootstrap-table/bootstrap-table.js"></script>
<script src="${ctxStatic}/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<script src="${ctxStatic}/bootstrap-table/extensions/editable/bootstrap-table-editable.js"></script>
<script src="${ctxStatic}/common/format/formatOrderData.js"></script>
<script type="text/javascript">
	$(function () {
		$("#inputForm").validate({
			submitHandler: function(form){
					postOrderDetil(form);
			},
			ignore: ":hidden",
			errorContainer: "#messageBox",
			errorPlacement: function(error, element) {
				$("#messageBox").text("输入有误，请先更正。");
				if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
					error.appendTo(element.parent().parent());
				} else {
					error.insertAfter(element);
				}
			}
		});
		
	    $('#tab').bootstrapTable({
	    	url: "${ctx}/order_detail/bsVaccineOrderDetail/getOrderDetailById",
	    	toolbar: "#toolbar",
	    	clickToSelect :true,
	    	uniqueId: "id",      //每一行的唯一标识，一般为主键列 
	    	queryParams: function (param) {
	            return {id :'${bsVaccineOrder.id}'};
	        } ,
	        columns: [{
	        	field:"选择",checkbox: true
	        }, {
	            field: 'id',
	            title: 'id',
	            visible: false
	        }, {
	            field: 'index',
	            title: 'index',
	            visible: false
	        }, {
	            field: 'batchnoId',
	            title: 'batchnoId',
	            visible: false
	        }, {
	            field: 'name',
	            title: '疫苗名称/注射器类型'
	        }, {
	            field: 'sort',
	            title: '属性',
	            formatter:formatSort
	        }, {
	            field: 'companyName',
	            title: '生产企业'
	        }, {
	            field: 'batchno',
	            title: '批号',
	            formatter:formatBatchno
	        }, {
	            field: 'spec',
	            title: '规格(剂/支或粒)',
	            formatter:formatSpec
	        }, {
	            field: 'dose',
	            title: '剂量'
	        }, {
	            field: 'vaccExpDate',
	            title: '有效日期',
	            formatter:formatDate
	        }, {
	            field: 'storenum',
	            title: '库存'
	        }, {
				field : 'storenumF',
				title : '可用库存',
				formatter : formatStorenumF
			}, {
	            field: 'num',
	            title: '报损数量',
	            width:'160px',
	            editable: {
	                type: 'text',
	                title: '出库数量',
	                emptytext : '0',
	                inputclass:'input-mini',
	                mode:'inline',
	                validate: function (v) {
	                	var num = parseInt(v)
	    				var num_reg = /^\+?[1-9][0-9]*$/;
	    				if (!num_reg.test(v)) {
	    					return '出库数量不合法';
	    				}
	                }
	            }
	        }, {
	            field: 'sellprice',
	            title: '出售价格'
	        }, {
	            field: 'orignprice',
	            title: '成本价格'
	        }, {
	            field: 'remarks',
	            title: '备注',
	            width:'150px',
	            editable: {
	                type: 'text',
	                title: '备注',
	                emptytext : '--',
	                inputclass:'input-mini',
	                mode:'inline',
	                validate: function (v) {
	                }
	            }
	        }]
	    });
	    

		$("#BtnDelete").click(function(){
			debugger;
			var deleteData = $('#deleteData').val();
			var ids = $.map($('#tab').bootstrapTable('getSelections'), function (row) {
				if (row.id!=''){
					deleteData = deleteData+row.id+","
					return row.id;
				}else{
					return row.index;
				}
            });
			$('#tab').bootstrapTable('remove', {
                field: 'id',
                values: ids
            });
			$('#tab').bootstrapTable('remove', {
                field: 'index',
                values: ids
            });
			$('#deleteData').val(deleteData);
		});
		
		$("#BtnNumShow").click(function(){
			var val2 = $('input:radio[name="cType"]:checked').val();
			if (!val2) {
				val2 = '';
				layer.msg("请先选择疫苗类型！");
			}
			var url = "${ctx}/vaccinate/bsProduct/getList?cType="+val2;
			layer.open({
				type : 2,
				title : '库存列表',
				shadeClose : true,
				shade : 0.8,
				area : [screen.availWidth * 0.7 + 'px', screen.availHeight * 0.6 + 'px' ],
				content : url
			});
		});
	});

	function layerDiv(obj) {
		var tr = obj;
		$('#tab').bootstrapTable('getData', false);
		var rows = $('#tab').bootstrapTable('getData');
		for(var i = 0 in rows){
			if(obj.batchnoId == rows[i].batchnoId){
				layer.msg("该批号已在出入库单中，请勿多次选择");
				return;
			}
		}
		$('#tab').bootstrapTable('append', tr);
	}

	function postOrderDetil(form) {//表单保存时  将此方法中的数据尽行保存
		//校验疫苗疫苗类型
		var val2 = $('input:radio[name="cType"]:checked').val();
		if (!val2) {
			$(".type-msg").find("font").text("必填信息");
			return;
		}
		var rows = $('#tab').bootstrapTable('getData');
		var rowIndex = rows.length;
		var result = false;
		debugger;
		if (rowIndex > 0) {
			for(var i = 0;i<rowIndex;i++ ){
				var num = parseInt(rows[i].num);
				var storenum = parseInt(rows[i].storenum);
				var storenumF = parseInt(rows[i].storenumF);
				var storenumK = storenum-storenumF;
				var num_reg = /^\+?[1-9][0-9]*$/;
				if ( rows[i].sort != val2){
					layer.msg("疫苗种类不符合，请确认疫苗种类");
					return;
				}
				if (!num_reg.test(rows[i].num)||num < 0 || num > storenum || num > storenumK) {
					layer.msg("数量不合法！请检查是否大于库存");
					return;
				}
				var sellprice_reg = /^\d+(\.\d+)?$/;
				if (!sellprice_reg.test(rows[i].sellprice)) {
					return '价格不合法!';
				}
			}
			$("#tableData").val(JSON.stringify(rows));
			layer.confirm('确定保存？', {
				icon: 3, title:'提示',
				btn : [ '保存', '取消' ]
			//按钮
			}, function() {
				layer.load(2);
				loading('正在提交，请稍等...');
				form.submit();
			});
		}else{
			layer.msg("未选择需要报损的疫苗");
		}
	}
	function printPdf(){
		window.open("${ctx}/order/bsVaccineOrder/print?id=${bsVaccineOrder.id}","_blank");
	}
	
	//注射器规格字典表
	var specDic = jQuery.parseJSON('${fns:getDictListJson("syringe_spec")}');
</script>
<style type="text/css">
input[type="checkbox"] {
    display: block;
}
</style>
 </head>
<body>
	<ul class="nav nav-tabs">
		<li class="active">
			<%-- <a href="${ctx}/order/bsVaccineOrder/addForm?orderType=${bsVaccineOrder.orderType}">疫苗使用</a> --%>
			<a href="#">疫苗报损</a>
		</li>
	</ul>
	<br />
	<form:form id="inputForm" modelAttribute="bsVaccineOrder" action="${ctx}/order/bsVaccineOrder/update" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<input id="tableData" type="hidden" name="tableData">
		<input id="deleteData" type="hidden" name="deleteData">
		<input id="supplyorgCode" type="hidden" name="supplyorgCode" value="${bsVaccineOrder.supplyorgCode}">
		<input id="orderType" type="hidden" name="orderType" value="${bsVaccineOrder.orderType}">
		<input id="orderStatus" type="hidden" name="orderStatus" value="${bsVaccineOrder.orderStatus}">
		<input id="tempStatus" type="hidden" name="tempStatus" value="${bsVaccineOrder.tempStatus}">
		<input id="createcode" type="hidden" name="createcode" value="${bsVaccineOrder.createcode}">
		<input id="createorgName" type="hidden" name="createorgName" value="${bsVaccineOrder.createorgName}">
		<sys:message content="${message}"/>
		<div class="row-fluid tools">
			<div class="control-group span6 minWidth secondChild">
				<label class="control-label">订单号：</label>
				<div class="controls">
					<form:input path="orderNo" htmlEscape="false" maxlength="50"
						readonly="true" class="span6" />
				</div>
			</div>
			<div class="control-group span6 minWidth">
				<label class="control-label">订单状态：</label>
				<div class="controls">
					<input name="orderStatusOld" type="text"  maxlength="20" class="span6 required" readonly
						value="${fns:getDictLabels(orderStatusOld, 'bs_order_status', '')}"/>
				</div>
			</div>
		</div>
		<div class="row-fluid">
			<div class="control-group span6 minWidth">
				<label class="control-label">使用日期：</label>
				<div class="controls">
					<input name="outboundDate" type="text"  maxlength="20" class="span6 Wdate required"
						value="<fmt:formatDate value="${bsVaccineOrder.outboundDate}" pattern="yyyy-MM-dd"/>"
						onclick="WdatePicker({dateFmt:'yyyy-MM-dd',isShowClear:true});"/>
					<span class="help-inline"><font color="red">*</font> </span>
				</div>
			</div>
			<div class="control-group span6 minWidth secondChild">
				<label class="control-label">类型：</label>
				<div class="controls">
					<form:select path="orderType"  readonly="true" disabled="true" class="span6 required ordertype">
						<form:options items="${fns:getDictList('bs_order_ordertype')}" itemLabel="label" itemValue="value" htmlEscape="false"/>
					</form:select>
					<span class="help-inline"><font color="red">*</font> </span>
				</div>
			</div>
		</div>
 		<div class="row-fluid tools">
			<div class="control-group span6 minWidth">
				<label class="control-label"><span class="help-inline"><font color="red">*</font> </span> 报损单位：</label>
				<div class="controls">
					<form:input path="supplyorgName" readonly="true" htmlEscape="false"
						maxlength="255" class="span6 required" />
				</div>
			</div>
			<div class="control-group span6 minWidth secondChild">
				<label class="control-label"><span class="help-inline"><font color="red">*</font> </span> 报损单位操作人：</label>
				<div class="controls">
					<form:input path="consignorName" readonly="false" htmlEscape="false" maxlength="50"
						class="span6 required" />
				</div>
			</div>
		</div>
		<div class="row-fluid">
			<div class="control-group span6">
				<label class="control-label"><span class="help-inline"><font color="red">*</font></span> 疫苗类型：</label>
				<div class="controls">
					<form:radiobuttons path="cType" items="${fns:getDictList('bs_vaccine_type')}"
						itemLabel="label" itemValue="value" class="required" />
					<span class="help-inline type-msg error"><font color="red"></font></span>
				</div>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注信息：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="3"
					style="width: 78%;resize: vertical;" maxlength="100"
					class="input-xlarge" placeholder="最多输入100个字"/>
			</div>
		</div>
		<div class="control-group">
			<table id="tab" class="table table-bordered table-condensed">
			</table>
		</div>
		<div id="toolbar">
			<button type="button" id = "BtnNumShow" class="btn btn-primary" >库存量查询</button>
			<button type="button" id = "BtnDelete" class="btn btn-primary">删除</button>
		</div>
		
		<div class="form-actions">
			<shiro:hasPermission name="order:bsVaccineOrder:edit">
				<button id="btnSubmit" class="btn btn-primary" type="submit">保存</button>
			</shiro:hasPermission>
			<button id="btnCancel" class="btn btn-primary" type="button" onclick="history.go(-1)">取消</button>
		</div>
	</form:form>
</body>
</html>